package com.xwj.dao;

import com.xwj.domain.OrderTxLog;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

/**
 * @author xwj
 * @date 2020/11/20
 */
@Mapper
@Repository
public interface OrderTxLogMapper {
    /**
     * 保存订单事务日志
     *
     * @param orderTxLog
     * @return
     */
    @Insert("insert into order_tx_log(xid,branch_id,status,create_time,update_time) values(#{xid},#{branchId},#{status},#{createTime},#{updateTime})")
    @Options(useGeneratedKeys = true, keyColumn = "id")
    int insert(OrderTxLog orderTxLog);

    /**
     * 根据全局事务id、分支事务id查询订单事务日志
     *
     * @param xid      全局事务id
     * @param branchId 分支事务id
     * @return
     */
    @Select("select id,xid,branch_id as branchId,status,create_time as createTime,update_time as updateTime " +
            "from order_tx_log where xid = #{xid} and branch_id = #{branchId} limit 1 for update")
    OrderTxLog selectOne(@Param("xid") String xid, @Param("branchId") long branchId);

    /**
     * 更新事务日志状态
     *
     * @param xid          全局事务id
     * @param branchId     分支事务id
     * @param expectStatus 预期状态
     * @param updateStatus 要更新的状态
     * @return
     */
    @Update("update order_tx_log set status = #{updateStatus} where xid = #{xid} and branch_id = #{branchId} and status = #{expectStatus}")
    int updateStatus(@Param("xid") String xid, @Param("branchId") Long branchId, @Param("expectStatus") Integer expectStatus, @Param("updateStatus") int updateStatus);
}
